package com.imoocdl.controller;

import com.imoocdl.VO.ResultVO;
import com.imoocdl.converter.OrderForm2OrderDTOConverter;
import com.imoocdl.dto.OrderDtDTO;
import com.imoocdl.enums.ResultEnum;
import com.imoocdl.exception.SellException;
import com.imoocdl.form.OrderForm;
import com.imoocdl.service.BuyerService;
import com.imoocdl.service.OrderService;
import com.imoocdl.utils.ResultVOUtil;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by pc on 2018/3/31.
 */
@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private BuyerService buyerService;
    //创建订单
    @PostMapping(value = "/create")
    public ResultVO<Map<String,String>> create(@Valid OrderForm orderForm, BindingResult bindingResult) throws SellException {
    if(bindingResult.hasErrors()){
        log.error("[创建订单] 参数不能正确 orderForm={}",orderForm);
        throw new SellException(ResultEnum.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
    }
        OrderDtDTO orderDtDTO = OrderForm2OrderDTOConverter.converter(orderForm);
        if(CollectionUtils.isEmpty(orderDtDTO.getOrderDetailList())){
            log.error("[创建订单] 购物车不能为空");
            throw new SellException(ResultEnum.CART_EMPTY);
        }
        OrderDtDTO createResult = orderService.create(orderDtDTO);
        Map<String,String> resultMap = new HashMap<>();
        resultMap.put("orderId",createResult.getOrderId());
        return ResultVOUtil.success(resultMap);
    }
    //订单列表
    @GetMapping(value = "/list")
    public ResultVO<List<OrderDtDTO>> list(@RequestParam(value = "openid")String openid
                    ,@RequestParam(value = "page",defaultValue = "0") Integer page
                    ,@RequestParam(value = "size",defaultValue = "10") Integer size){
        if(StringUtils.isEmpty(openid)){
            log.error("[查询订单列表] openid为空");
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        PageRequest pageRequest = new PageRequest(page,size);
        Page<OrderDtDTO> orderDtDTOPage = orderService.findList(openid,pageRequest);
        return ResultVOUtil.success(orderDtDTOPage.getContent());
    }

    //订单详情
    @GetMapping(value = "/detail")
    public ResultVO<OrderDtDTO> detail(@RequestParam(value = "openid") String openid
                        ,@RequestParam(value = "orderId") String orderId){
        OrderDtDTO orderDtDTO = buyerService.findOrderOne(openid,orderId);
        return ResultVOUtil.success(orderDtDTO);
    }

    //取消订单
    @PostMapping(value = "/cancel")
    public ResultVO cancel(@RequestParam(value = "openid") String openid
            ,@RequestParam(value = "orderId") String orderId){
      buyerService.cancelOrder(openid,orderId);
        return ResultVOUtil.success();
    }

}
